class Solution {
public:
    int longestArithSeqLength(vector<int>& nums) {
        int n=nums.size();
        if(n<3) return n;
        int ret=2;
        vector<vector<int>> dp(n,vector<int>(n,2));
        for(int j=1;j<n;++j)
        {
            unordered_map<int,int> hash(n);
            for(int i=0;i<j;++i)
            {
                int x=nums[j]-nums[i];
                if(hash.count(nums[i]-x)&&hash[nums[i]-x]<i)
                    dp[i][j]=dp[hash[nums[i]-x]][i]+1;
                ret=max(ret,dp[i][j]);
                hash[nums[i]]=i;
            }
        }
        return ret;
    }
};
